$(function() {
	initPage();
	initButton();
});

 //初始化页面
function initPage() {
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/info/getTeaInfo.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			console.log(result.data);
			if (result.retCode == '0000') {
				if(result.data.mobile == null || "" == result.data.mobile){
					$("#myPhone").attr("placeholder","请输入要绑定的手机号");
					//$("#weixin").addClass("binding");
				}else{
					$("#myPhone").val(result.data.mobile);
				}
				
			} else{
				showErrorMsg(result.data);
			}
		}
	});
	
	
	
}


/**
 * 点击事件
 */
function initButton() {
	//提交按钮
	$("#upmobile").bind('click', function() {
		var mobile = $("#myPhone").val()
		if(!checkNull(mobile,"请输入要绑定的手机号")){
			return;
		}
		var qqq = checkCode();
		if (checkCode() == false) {
			return;
		}else{
			$.ajax({
				type : "post",
				url : server_url
				+ 'rest/tea/info/updateWeixinForuser.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					usersid:localStorage.getItem('userid'),
					mobile:mobile
				},
				success : function(result) {
					//console.log(result);
					if (result.retCode == '0000') {
						window.location=server_url+"mySet3.do";
					} else{
						showErrorMsg(result.data);
					}
				}
			});
		}
	});
	
	
}


//检查验证码
function checkCode() {
	var codeUuid = localStorage.getItem("phoneNoUUID");
	var code = $("input[name='code']").val();
	$.ajax({
		type : "POST",
		dataType : 'json',
		async : false,
		url : server_url + 'rest/frontUser/checkCode.do?',
		data : {
			codeUuid : codeUuid,
			code : code,
			ran : Math.random()
		},
		success : function(data) {
			if (data == 1) {
				return true;
			} else if (data == 0) {
				alert("验证码错误");
				return false;
			}
		}
	});
}

